import { defineStore } from 'pinia';
import { store } from '@/store';
import { IParam } from '@/core';

interface ViewCtx {
	appGlobal: IParam;
	routeViewGlobal: IParam;
	appViews: IParam,
}

const useViewCtxStore = defineStore({
	id: 'viewCtx',
	state: (): ViewCtx => ({
		appGlobal: {},
		routeViewGlobal: {},
		appViews: {},
	}),
	getters: {
		getAppGlobal() {
			return () => {
				return this.appGlobal;
			};
		},
		getAppGlobalByTag() {
			return (tag: string) => {
				return this.appGlobal[tag] || {};
			};
		},
		getRouteViewGlobal() {
			return () => {
				return this.routeViewGlobal;
			};
		},
		getRouteViewGlobalByTag() {
			return (tag: string) => {
				return this.routeViewGlobal[tag] || {};
			};
		},
		getView() {
			return (tag: string) => {
				return this.appViews[tag] || undefined;
			};
		},
	},
	actions: {
		initAppGlobal(param: IParam) {
			this.appGlobal = param;
		},
		setAppGlobal(tag: string, param: IParam) {
			this.appGlobal[tag] = param;
		},
		setRouteViewGlobal(tag: string, param: IParam) {
			this.routeViewGlobal[tag] = param;
			if (tag) {
				if (!this.routeViewGlobal.hasOwnProperty(tag)) {
					this.routeViewGlobal[tag] = param;
				} else {
					const tempParam = this.routeViewGlobal[tag];
					Object.assign(tempParam, param);
					this.routeViewGlobal[tag] = tempParam;
				}
			}
		},
		setView(tag: string, param: IParam) {
			this.appViews[tag] = param;
		}
	},
});

export function useViewCtxStoreWithOut() {
	return useViewCtxStore(store);
}
